from ..block.sources import SourcesItem
from .base import BaseExportEngine


TEMPLATE = r"""

{{{review}}}

\begin{thebibliography}{9}

{{{bibliography}}}

\end{thebibliography}

"""


class LatexExportEngine(BaseExportEngine):
    def prepare_bibliography(self, state_data: dict) -> str:
        sources = [
            SourcesItem.model_validate(item)
            for item in state_data.get('sources', [])
        ]
        results = [
            item.bibtex
            for item in sources
        ]
        return "\n".join(results)

    def export(self, state_data: dict) -> str:
        bibliography = self.prepare_bibliography(state_data)
        replacements = {
            'review': state_data.get('review', ''),
            'bibliography': bibliography,
        }
        result = TEMPLATE
        for k, v in replacements.items():
            result = result.replace('{{{' + k + '}}}', v)
        return result
